<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>品牌查询</title>
    <script src="js/vue.js"></script>
    <script src="js/axios-0.18.0.js"></script>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>
</head>
<body>
<div id="app">
    <!--搜索-->
    <el-row>
        <el-col :span="20">
            <!--搜索表单-->
            <el-form :inline="true" :model="condition" class="demo-form-inline">

                <el-form-item label="当前状态">
                    <el-select v-model="condition.status" placeholder="当前状态" style="width: 120px">
                        <el-option label="启用" value="1"></el-option>
                        <el-option label="禁用" value="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="企业名称">
                    <el-input v-model="condition.companyName" placeholder="企业名称"></el-input>
                </el-form-item>

                <el-form-item label="品牌名称">
                    <el-input v-model="condition.brandName" placeholder="品牌名称"></el-input>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--按钮-->
        <el-col :span="4">
            <el-button type="danger" plain @click="deleteByIds">批量删除</el-button>
            <!--打开添加的窗口-->
            <el-button type="primary" plain @click="openAddBrand">新增</el-button>
        </el-col>

    </el-row>

    <!--添加品牌信息的弹窗-->
    <el-dialog
            title="添加品牌"
            :visible.sync="showAdd"
            width="30%">

        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           active-value="1"
                           inactive-value="0"
                ></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="showAdd = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--修改品牌信息的弹窗-->
    <el-dialog
            title="修改品牌信息"
            :visible.sync="showAlter"
            width="30%">

        <el-form ref="form" :model="brand" label-width="80px">
            <el-form-item label="品牌名称">
                <el-input v-model="brand.brandName"></el-input>
            </el-form-item>

            <el-form-item label="企业名称">
                <el-input v-model="brand.companyName"></el-input>
            </el-form-item>

            <el-form-item label="排序">
                <el-input v-model="brand.ordered"></el-input>
            </el-form-item>

            <el-form-item label="备注">
                <el-input type="textarea" v-model="brand.description"></el-input>
            </el-form-item>

            <el-form-item label="状态">
                <el-switch v-model="brand.status"
                           :active-value="1"
                           :inactive-value="0"
                ></el-switch>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="submitAlterBrand">提交</el-button>
                <el-button @click="showAlter = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>

    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
                @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index" :index="calcIndex"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="id"
                    label="品牌id"
                    align="center"
            >
            </el-table-column>

            <el-table-column
                    prop="brandName"
                    label="品牌名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="companyName"
                    label="企业名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="ordered"
                    align="center"
                    label="排序">
            </el-table-column>
            <el-table-column
                    prop="description"
                    label="备注"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="status"
                    align="center"
                    label="是否激活"
                    >
                            <!--绑定一个点击的方法  改变状态 传入当前行的数据-->
                <template slot-scope="scope">
                    <el-switch
                            @click="changeStatus(scope.row)"
                            :value="scope.row.status==1"
                            active-text="是"
                            inactive-text="否">
                    </el-switch>
                </template>
                </el-switch>
            </el-table-column>

            <el-table-column
                    align="center"
                    label="操作">
                <!--做一个标签加了scope属性就能获取到当前行的数据-->
                <template slot-scope="scope">
                    <el-row>
                        <!--传入当前的的品牌对象-->
                        <el-button type="primary" @click="openAlterBrand(scope.row)">修改</el-button>
                        <!--传入当前对象的id-->
                        <el-button type="danger" @click="deleteOneBrands(scope.row.id,scope.row.brandName)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>


<script src="js/vue.js"></script>
<script src="element-ui/lib/index.js"></script>
<link rel="stylesheet" href="element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        methods: {
            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            //点击改变状态
            changeStatus(row){
                let status = row.status;
                if(status){
                    //如果为1就变成0
                    row.status = 0;
                }else {
                    row.status = 1;
                }
                //将修改提交到数据库
                axios
                    .post(`brand/updateStatus?id=${row.id}&status=${row.status}`)
                    .then((resp)=>{
                        //查一手
                        this.selectByPage();
                    })
            },

            // 批量删除
            deleteByIds() {
                //如果未选中品牌就干掉方法  并且提示
                if(this.selectedIds.length==0){
                    this.$message("请选择要删除的品牌!");
                    return;
                }
                let choose = confirm("你确定要删除这些品牌吗?");
                if(choose){
                //将selectedIds
                let ids = this.selectedIds;
                //alert(ids);
                //将选中的id数组发送
                axios
                    .post("brand/deleteByIds",ids)
                    .then((response)=>{
                        let data = response.data;
                        if(data!=0){
                            this.$message.success("删除成功!");
                            //清空选择区域的数组
                            this.selectedIds = [];
                            //重新加载页面
                            this.selectByPage();
                        }
                    })
                }
            },

            // 复选框选中后执行的方法  传的参数是被选中的品牌的全部数据
            handleSelectionChange(brands) {
                //将选中的id添加到id数组中
                for (let i = 0; i < brands.length; i++) {
                    //只将id添加进去
                    this.selectedIds[i] = brands[i].id;
                }
            },

            calcIndex(index) {
                return (this.currentPage - 1) * this.pageSize + index + 1;
            },

            //单个点击删除
            deleteOneBrands(id,name){
                let choose = confirm(`你确定要删除${name}这个品牌吗?`);
                if(choose){
                    //确定删除
                    axios
                        .post("brand/delete",id)
                        .then((response)=>{
                            let data = response.data;
                            if(data!=0){
                                //删除完毕
                                alert("删除成功!")
                                //重新查询
                                this.selectByPage();
                            }
                        })
                }

            },

            // 搜索的方法
            onSubmit() {
                /*
                * 点击搜索会 出bug
                * 点击搜索时查询的条件condition已经和查询框的绑定了
                * 但是如果当前的页码在非常靠后,但是输入的条件只又很少几条
                * 如果直接调用分页查找this.selectByPage();他的页码不会改变就有可能显示不了数据
                * 所有我们要将页码重置为1再调用分页查询功能
                * */
                //用户输入的查询条件
                /*let condition = this.condition;
                axios
                    .post("SelectByBrandServlet",condition)
                    .then((response)=>{
                        this.tableData = response.data;
                    })*/
                //将页码重置为1
                this.currentPage = 1;
                //调用分页查询
                this.selectByPage();
            },

            //修改当前每页显示多少条记录
            handleSizeChange(val) { //我们选择的是 ‘5条/页’ 此值就是 5.而我们选择了 `10条/页` 此值就是 10
                this.pageSize = val;
                this.selectByPage();
            },

            //当点击某个页面后跳转页面的方法
            handleCurrentChange(val) { //val 就是改变后的页码
                //吧改变后的值赋值给页码的模型
                this.currentPage = val;
                //模型改变后再查询一次
                this.selectByPage();
            },

            //打开添加数据的窗口
            openAddBrand(){
                //首先将添加的文本框显示出来
                //当吧el-dialog标签的visible.syn属性设置为true的时候显示
                this.showAdd = true;
            },

            //分页查询
            selectByPage(){
                //当前页码
                let pageIndex = this.currentPage;
                //当前页面的大小
                let pageSize = this.pageSize;
                //当前页面的查询条件
                let brand = this.condition;
                //发送查询条件
                axios
                    .post(`brand/selectByPag?pageIndex=${pageIndex}&pageSize=${pageSize}`,brand)
                    .then((response)=>{
                        //得到返回的pageBean对象
                        let data = response.data;
                        //设置当前表中显示的数据为符合条件的品牌信息
                        this.tableData = data.brands;
                        //设置当前页面的总条数
                        this.totalCount = data.total;
                    })
            },

            // 提交添加数据
            addBrand() {
                //获取用户输入的信息绑定在brand上面
                let brand = this.brand;
                //将用户输入的信息发送给servlet
                axios
                    .post("brand/add", brand)
                    .then((response) => {
                        let data = response.data;
                        if(data=="success"){
                            //添加完成清空输入框并且将添加窗口隐藏
                            //隐藏
                            this.showAdd = false;
                            //将一个空对象赋值给他
                            this.brand = {};
                            //重新查询全部数据并且展示
                            //收到修该添加成功的消息在查询一次全部
                            alert("添加成功!");
                            this.selectByPage();
                        }
                    })
            },

            //打开修改页面并且回显当前行是数据
            openAlterBrand(brand) {
                //首先将修改品牌的对话框弹出
                this.showAlter = true;
                //通过id访问呢数据库取出当前行的品牌对象
                this.brand = brand;
            },

            //提交修改
            submitAlterBrand(){
                //获得用户输入的信息
                let brand = this.brand;
                //将修改后的信息发送出去
                axios
                    .post("brand/update",brand)
                    .then((response)=>{
                        let data = response.data;
                        if(data!=0){
                            //将文本清空
                            this.brand = {};
                            //将文本框隐藏
                            this.showAlter = false;
                            //收到消息在查询全部
                            alert("修改成功!");
                            this.selectByPage();
                        }
                    })
            },

            //查询所有品牌
            selectAll() {
                //加载完页面后调用这个查询所有品牌的方法
                //异步请求
                axios
                    .post("brand/selectAll")
                    .then((response) => {
                        this.tableData = response.data;
                    })
            }
        },
        //当页面加载完成后，发送异步请求，获取数据
        mounted() {
            //this.selectAll();
            this.selectByPage();
        },
        data() {
            return {
                // 当前页码
                currentPage: 1,
                //每页大小
                pageSize: 5,
                //总记录数
                totalCount: 10,
                // 添加数据对话框是否展示的标记
                showAdd: false,
                //修改数据对话框是否展示的标记
                showAlter: false,
                // 查询条件
                condition: {},
                // 品牌模型数据
                brand: {
                    status: '',
                    brandName: '',
                    companyName: '',
                    id: "",
                    ordered: "",
                    description: ""
                },
                // 复选框选中数据集合
                multipleSelection: [],
                // 选中要删除的id数组
                selectedIds: [],
                // 表格数据
                tableData: [{
                    id: 9999,
                    brandName: "老王",
                    companyName: "老驴",
                    ordered: 15,
                    description: "你是一头驴",
                    status: 1
                },{
                    id: 9999,
                    brandName: "老王",
                    companyName: "老驴",
                    ordered: 15,
                    description: "你是一头驴",
                    status: 0
                }]
            }
        }
    })

</script>

</body>
</html>